package com.zzq.aop;

import com.zzq.domain.entity.SmileClient;
import com.zzq.domain.entity.SmilePointRecord;
import com.zzq.domain.entity.SmilePointRule;
import com.zzq.enumerate.IncomeExpenditureStatusEnum;
import com.zzq.result.SmileResult;
import com.zzq.security.util.UserUtil;
import com.zzq.service.SmileClientService;
import com.zzq.service.SmilePointRecordService;
import com.zzq.service.SmilePointRuleService;
import com.zzq.util.ObjectUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

import static java.awt.SystemColor.info;

/**
 * 客户 积分AOP
 */
@Aspect
@Component
@EnableAspectJAutoProxy
public class SmilePointAop {
   private final   SmilePointRuleService pointRuleService;
    @Autowired
    private  SmilePointRecordService pointRecordService;
    @Autowired
    private SmileClientService clientService;

    private final   List<SmilePointRule> pointRuleList;
    @Autowired
    public SmilePointAop(SmilePointRuleService pointRuleService) {
        this.pointRuleService = pointRuleService;
        pointRuleList=  pointRuleService.list();
    }

    @Around("execution(* com.zzq.subservice.impl.EarnPointsSubServiceImpl.*(..))")
    public Object dynamicPointcut(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        if (proceed instanceof SmilePointRecord){
            SmilePointRecord record=(SmilePointRecord) proceed;
            String incomeExpenditureStatus = record.getIncomeExpenditureStatus();
            Integer points = record.getPoints();
            Long clientId = record.getClientId();
            SmileClient client = clientService.getById(clientId);
            Integer newPoints=0;
            if (incomeExpenditureStatus.equals(IncomeExpenditureStatusEnum.INCOME.getName())) {
                newPoints = client.getClientPoints() + points;
                client.setClientPoints(newPoints);
            }else {
                newPoints = client.getClientPoints() - points;
                client.setClientPoints(newPoints);
            }
            clientService.setInfo(client);
        }
        return proceed;
    }



}
